package com.learn.egweibo2.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.learn.egweibo2.base.response.JsonResult;
import com.learn.egweibo2.base.response.StatusCode;
import com.learn.egweibo2.mapper.CommentMapper;
import com.learn.egweibo2.pojo.dto.CommentParam;
import com.learn.egweibo2.pojo.entity.Comment;
import com.learn.egweibo2.pojo.vo.CommentVO;
import com.learn.egweibo2.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment/")
@Api(tags = "03.评论管理模块")
@Slf4j
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation("发布评论")
    @ApiOperationSupport(order = 10)
    public JsonResult insertComment(@RequestBody CommentParam commentParam, @ApiIgnore HttpSession session){
        log.debug("commentParam:" +commentParam);
        log.debug("============");
        UserVO userVO = (UserVO)session.getAttribute("user");
        /*if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/

        log.debug("============");
        Comment comment = new Comment();
        comment.setContent(commentParam.getComment());
        comment.setWeiboId(commentParam.getWeiboId());
        comment.setUserId(userVO.getId());
        comment.setCreateTime(new Date());
        log.debug("comment:"+comment);

        int rs = commentMapper.insertComment(comment);
        log.debug(rs>0?"评论成功":"评论失败");

        return JsonResult.ok();
    }

    @GetMapping("selectByWeiboId")
    @ApiOperation("获取评论列表")
    @ApiOperationSupport(order = 20)
    @ApiImplicitParam(name = "weiboId", value = "微博id", required = true, dataType = "Long", paramType = "query")
    public JsonResult selectCommentByWeiboId(Long weiboId){
        log.debug("weiboId:"+weiboId);

        List<CommentVO> commentVOS = commentMapper.selectCommentByWeiboId(weiboId);

        return JsonResult.ok(commentVOS);
    }
}
